﻿
Partial Class WebForm_ManageRole_OpenManageRoleBrandMappingV3
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack() Then
            Me.BindViewPoint()
            btnApply.Visible = False
        End If
    End Sub
    Protected Sub btQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btQuery.Click
        Me.mpSearch.Show()
        btnApply.Visible = True
        Me.BindHierarchy()
        Me.tvHierarchy.Attributes.Add("onClick", "OnCheckEvent()")
    End Sub
    Protected Sub btnApply_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnApply.Click
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        If ProdHierarchy <> "" Then
     
            For Each PHString As String In ProdHierarchy.Split(",")
                Select Case PHString.Length
                    Case "2"
                    Case "4"
                    Case "6"
                    Case "8"
                    Case "10"
                    Case "14"
                    Case "16"
                        PHString = PHString & New String("X", 18 - PHString.Length)
                    Case "12"
                        PHString = New String("X", 8) & PHString.Substring(9, 2).ToString() & New String("X", 8)
                    Case "18"
                        PHString = PHString
                End Select
                sql.Length = 0
                sql.AppendFormat("delete from RoleHierarchyMapping ")
                sql.AppendFormat("where RoleID='{0}' ", Me.RoleID)
                sql.AppendFormat("and ProdHierarchy='{0}' ", PHString)
                sql.AppendFormat("and ViewPoint='{0}' ", ViewPoint)
                sql.AppendFormat(";insert into RoleHierarchyMapping(RoleID,ProdHierarchy,ViewPoint) ")
                sql.AppendFormat("Select ")
                sql.AppendFormat("'{0}' ", Me.RoleID)
                sql.AppendFormat(",'{0}' ", PHString)
                sql.AppendFormat(",'{0}' ", ViewPoint)
                sql.AppendFormat("; ")


            Next
            dat.AccData(sql.ToString())
            MyObj.ShowMessage("Apply Successful!")
        End If
    End Sub

    Protected Sub tvHierarchy_TreeNodeCheckChanged(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles tvHierarchy.TreeNodeCheckChanged

        For Each node As TreeNode In tvHierarchy.CheckedNodes
            If node.Text <> FirstNodeString Then
                ProdHierarchy &= node.Value & ","
            Else
                ProdHierarchy = ""
            End If
        Next

        '去除最後的逗號
        If ProdHierarchy <> "" Then
            ProdHierarchy = ProdHierarchy.Substring(0, ProdHierarchy.Length - 1)
        End If

    End Sub
#Region "方法"
    Public Sub InitDataAndShow()
        Me.mpSearch.Show()
        RoleID = Me.RoleID
        Dim sql As New MyStringBuilder
        Dim dat As New clsData
        sql.AppendFormat("Select * from Role ")
        sql.AppendFormat("where RoleID = '{0}' ", RoleID)

        Division = dat.GetData(sql).Rows(0)("Division").ToString()


    End Sub
#End Region

#Region "繫結資料"
    Dim MyDT As DataTable
    Private Sub BindHierarchy()
        Me.tvHierarchy.Visible = True
        Me.tvHierarchy.Nodes.Clear()

        Dim sql As New MyStringBuilder
        Dim dat As New clsData
        sql.AppendFormat("Select distinct m.* From MaintainLevel m ")
        sql.AppendFormat("Where m.IsSelect = 'Y' ")
        sql.AppendFormat("and m.Division = '{0}' ", Division)
        sql.AppendFormat("and m.ViewPoint = '{0}' ", ddlViewPoint.SelectedValue)
        sql.AppendFormat("order by m.Sequence ")
        MyDT = dat.GetData(sql)

        If MyDT.Rows.Count > 0 Then
            Dim node As New TreeNode

            node.Text = MyDT.Rows(0)("HierarchyName").ToString()
            node.Value = "null"
            FirstNodeString = node.Text
            Me.tvHierarchy.Nodes.Add(node)

            Me.BindHierarchy(node, 0)

            Me.tvHierarchy.ExpandAll()

        End If

    End Sub

    Private Sub BindHierarchy(ByVal node As TreeNode, ByVal RowIndex As Integer)
        If RowIndex > 2 Then Return

        Dim sql As New MyStringBuilder
        Dim dat As New clsData
        Dim dt As New Data.DataTable
        Dim dtTree As New Data.DataTable

        sql.Length = 0
        If RowIndex = 0 Then
            sql.AppendFormat("select * from ProductHierarchyOriginal a  ")
            sql.AppendFormat("where a.LevelName='{0}' ", node.Text)
            sql.AppendFormat("and a.Code in (select Brand from DivisionBrandMappingView where Division ='{0}') ", Division)

        ElseIf MyDT.Rows(RowIndex)("HierarchyName").ToString() = "Axe" Then
            sql.AppendFormat("select distinct a.Description,left(a.code,2) as Code from ProductHierarchyOriginal a ")
            sql.AppendFormat("join ProductLevel b ")
            sql.AppendFormat("on a.LevelName=b.LevelName ")
            sql.AppendFormat("and b.LevelName='{0}' ", MyDT.Rows(RowIndex)("HierarchyName").ToString())
            sql.AppendFormat("join ProductHierarchy_All al on ")
            sql.AppendFormat("left(a.code,2)=left(al.prodhierarchy,2) ")
            sql.AppendFormat("where left(a.code,2)= '{0}'  ", node.Value)

        Else
            sql.AppendFormat("select distinct a.Description,a.Code from ProductHierarchyOriginal a ")
            sql.AppendFormat("join ProductLevel b ")
            sql.AppendFormat("on a.LevelName=b.LevelName ")
            sql.AppendFormat("and b.LevelName='{0}' ", MyDT.Rows(RowIndex)("HierarchyName").ToString())
            sql.AppendFormat("join ProductHierarchy_All al on  ")
            sql.AppendFormat("substring(a.Code,1,(b.Sequence-1)*2)=substring(al.prodhierarchy,1,(b.Sequence-1)*2) ")
            sql.AppendFormat("where substring(a.Code,1,(b.Sequence-1)*2)='{0}' ", node.Value)

        End If

        dtTree = dat.GetData(sql)

        If dtTree.Rows.Count > 0 Then
            For Each dr As Data.DataRow In dtTree.Rows
                Dim child As New TreeNode()
                child.Text = dr("Description")
                child.Value = dr("Code")

                node.ChildNodes.Add(child)
                Me.BindHierarchy(child, RowIndex + 1)
            Next
        End If

    End Sub

    Protected Sub BindViewPoint()
        ddlViewPoint.DataTextField = "Text"
        ddlViewPoint.DataValueField = "Value"

        ddlViewPoint.DataSource = MyObj.GetCodeMappingData("Product", "ViewPoint")
        ddlViewPoint.DataBind()
        ddlViewPoint.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())
    End Sub
#End Region

#Region "屬性"
    Public Property FirstNodeString() As String
        Get
            Return ViewState("FirstNodeString")
        End Get
        Set(ByVal value As String)
            ViewState("FirstNodeString") = value
        End Set
    End Property
    Public Property ProdHierarchy() As String
        Get
            Return ViewState("ProdHierarchy")
        End Get
        Set(ByVal value As String)
            ViewState("ProdHierarchy") = value
        End Set
    End Property
    Public Property ViewPoint() As String
        Get
            Return Me.ddlViewPoint.SelectedValue
        End Get
        Set(ByVal value As String)
            Me.ddlViewPoint.SelectedValue = value
        End Set
    End Property
    Public Property Division() As String
        Get
            Return ViewState("Division")
        End Get
        Set(ByVal value As String)
            ViewState("Division") = value
        End Set
    End Property
    Public Property RoleID() As Integer
        Get
            Return ViewState("RoleID")
        End Get
        Set(ByVal value As Integer)
            ViewState("RoleID") = value
        End Set
    End Property
#End Region

End Class
